function encodeURL() {
    const input = document.getElementById('url-input').value;
    const output = document.getElementById('url-output');

    if (!input) {
        showURLMessage('请输入需要编码的文本', 'error');
        return;
    }

    try {
        const result = encodeURIComponent(input);
        output.value = result;
        showURLMessage('编码成功', 'success');
    } catch (err) {
        showURLMessage(`编码失败: ${err.message}`, 'error');
        output.value = '';
    }
}

function decodeURL() {
    const input = document.getElementById('url-input').value;
    const output = document.getElementById('url-output');

    if (!input) {
        showURLMessage('请输入需要解码的文本', 'error');
        return;
    }

    try {
        const result = decodeURIComponent(input);
        output.value = result;
        showURLMessage('解码成功', 'success');
    } catch (err) {
        showURLMessage(`解码失败: ${err.message}`, 'error');
        output.value = '';
    }
}

function showURLMessage(message, type = 'error') {
    const messageEl = document.getElementById('url-message');
    messageEl.textContent = message;
    messageEl.className = `message ${type} show`;
    setTimeout(() => messageEl.classList.remove('show'), 3000);
}

async function copyURLResult() {
    const output = document.getElementById('url-output').value;
    if (!output) {
        showURLMessage('没有可复制的内容', 'error');
        return;
    }

    try {
        await navigator.clipboard.writeText(output);
        showURLMessage('已复制到剪贴板', 'success');
    } catch (err) {
        showURLMessage('复制失败', 'error');
    }
}

function clearURL() {
    document.getElementById('url-input').value = '';
    document.getElementById('url-output').value = '';
    document.getElementById('url-message').classList.remove('show');
}
